<?php

if(!defined('EXECROOT')) {
	define('EXECROOT','');
}

class lcarsTextBuffer extends GtkTextBuffer {

	public $scrollpos;
	private $viewer;
	private $linecount;
	private $width,$height;
	private $putx,$puty;
	public $atBottom;

	function __construct() {
		parent::__construct();
		$this->scrollpos = $this->width = $this->height = 0;
		$this->viewer = null;
		$this->atBottom = true;
		$this->connect_simple_after('changed',array($this,'onChange'));
		return;
	}
	
	public function append($text) {
		$this->linecount += substr_count($text,chr(10));
		$this->place_cursor($this->get_end_iter());
		$this->insert_at_cursor("\n".trim($text));

		if($this->linecount > 100) {
			$this->trimBuffer();
		}		
		
		lcars::killTime();

		if($this->viewer) {
			//. scroll position saving?
		}		
		
		return;	
	}
	
	public function trimBuffer() {

		$end = $this->linecount - 100;
		$enditer = $this->get_iter_at_line($end);
		
		$this->delete($this->get_start_iter(),$enditer);
		$this->linecount -= $end;	
	
		return;
	}
	
	
	public function onChange() {
		if($this->atBottom && is_object($this->viewer)) {
			Gtk::timeout_add(100,array($this->viewer,'scrollToBottom'));
		}
		return;
	}
	
	public function setViewer($view) {
		$this->viewer = $view;
		return;
	}

}

class lcarsTextView extends lcarsScroller {

	const SCROLL_DOWN = 1;
	const SCROLL_UP = 2;
	
	private $scroll;
	private $colourNormal;

	public function __construct($colour = '#ffffff') {
		parent::__construct();

		$this->scroll = null;
		$this->view = new GtkTextView;
		$this->scrlbox->add($this->view);
	
		$this->view->set_cursor_visible(false);
		$this->view->set_editable(false);
		$this->view->set_wrap_mode(Gtk::WRAP_WORD_CHAR);	
		
		$this->colourNormal = $colour;
		$this->view->modify_base(Gtk::STATE_NORMAL,GdkColor::parse('#000000'));
		$this->view->modify_text(Gtk::STATE_NORMAL,GdkColor::parse($this->colourNormal));
		$this->view->modify_font(new PangoFontDescription('Monospace'));
//		$this->view->modify_font(new PangoFontDescription('Swiss911 XCm BT Condensed 13'));
		lcars::killTime();
		
		$this->btnup->connect_simple('button-press-event',array($this,'scrollBufferUp'));
		$this->btntop->connect_simple('button-press-event',array($this,'scrollBufferUp'));
		$this->btnbtm->connect_simple('button-press-event',array($this,'scrollBufferBottom'));

		// testing
		$this->buffer = new lcarsTextBuffer;
		$this->defaultbuffer = $this->buffer;
		$this->buffer->setViewer($this);
		$this->view->set_buffer($this->buffer);
		
		$this->defaultbuffer->append(commnet::$VERSION);
		
		$this->show_all();
		return;
	}
	
	public function scrollBufferUp() {
		$buffer = $this->view->get_buffer();
		$buffer->atBottom = false;
		return;
	}

	public function scrollBufferBottom() {
		$buffer = $this->view->get_buffer();
		$buffer->atBottom = true;
		return;
	}
	
	public function setColour($colour) {
		if($colour === false) {
			$this->view->modify_text(Gtk::STATE_NORMAL,GdkColor::parse($this->colourNormal));		
		} else {
			$this->view->modify_text(Gtk::STATE_NORMAL,GdkColor::parse($colour));
		}
		return;
	}
	
	public function setTerminal($buffer) {
		$this->view->set_buffer($buffer);
		$this->buffer->setViewer = null;
		$this->buffer = null;
		
		$buffer->setViewer($this);
		$this->buffer = $buffer;
		return;
	}
}

?>